package com.estar.assistsurvy.adapter;

import java.io.File;
import java.util.ArrayList;

import com.bumptech.glide.Glide;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.ScalePagerItem;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class PicturePagerAdapter extends PagerAdapter {

	private ArrayList<View> views;
	private ArrayList<ScalePagerItem> list;
	private Context context;
	
	public PicturePagerAdapter(ArrayList<ScalePagerItem> list, Context context) {
		super();
		this.list=list;
		this.context=context;
		views=new ArrayList<>();
		for(int i=0;i<list.size();i++){
			View view=LayoutInflater.from(context).inflate(R.layout.picture_viewpager, null);
			
			views.add(view);
		}
	}

//	public void setViews(ArrayList<ScalePagerItem> list,Context context) {
//		views=new ArrayList<>();
//		for(int i=0;i<list.size();i++){
//			View view=LayoutInflater.from(context).inflate(R.layout.picture_viewpager, null);
//			ImageView scalePic=(ImageView) view.findViewById(R.id.scalePic);
//			if(list.get(i).getDrawables()!=null){
//				scalePic.setImageDrawable(list.get(i).getDrawables());
//			}
//			ImageView backImage=(ImageView) view.findViewById(R.id.backImage);
//			backImage.setImageResource(list.get(i).getScaleImages());
//			TextView title=(TextView) view.findViewById(R.id.title);
//			title.setText(list.get(i).getTitles());
//			views.add(view);
//		}
//	}

	@Override
	public int getCount() {
		return views.size();
	}
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {

		((ViewPager)container).removeView(views.get(position));
	}

	@Override
	public Object instantiateItem(View container, int position) {
		((ViewPager)container).addView(views.get(position));
		ImageView scalePic=(ImageView) views.get(position).findViewById(R.id.scalePic);
		if(list.get(position).getPicPath()!=null){
//			scalePic.setImageDrawable(list.get(position).getDrawables());
			File file=new File(list.get(position).getPicPath());
			Glide.with(context).load(file).into(scalePic);
		}
		ImageView backImage=(ImageView) views.get(position).findViewById(R.id.backImage);
//		backImage.setImageResource(list.get(position).getScaleImages());
		Glide.with(context).load(list.get(position).getScaleImages()).into(backImage);
		TextView title=(TextView) views.get(position).findViewById(R.id.title);
		title.setText(list.get(position).getTitles());
		return views.get(position);
	}
	
	@Override    
    public void finishUpdate(View arg0) {}    
        
    @Override  
    public void restoreState(android.os.Parcelable state, ClassLoader loader) {  
       
    };  

    @Override    
    public Parcelable saveState() {    
        return null;    
    }    

    @Override    
    public void startUpdate(View arg0) {}    
    
    
    private int mChildCount = 0;
    
    @Override
    public void notifyDataSetChanged() {         
          mChildCount = getCount();
          super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object)   {          
          if ( mChildCount > 0) {
          mChildCount --;
          return POSITION_NONE;
          }
          return super.getItemPosition(object);
    }

}
